﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MinhTuan;
namespace Products
{
    public class Orders
    {
        #region // attribute
                private Int32 _Order_Id;
                private Int32 _Members_Id;
                private DateTime _Order_Date;
                private Int32 _Order_Quantity;
                private Double _Order_Total;
                private String _Shipping_Name;
                private String _Shipping_Address;
                private String _Shipping_Tel;
                private String _Shipping_City;
                private String _Shipping_Notice;
        #endregion
        #region //initialize
                    public Orders()
                    {

                    }
                    public Orders(
			            Int32 Order_Id,
			            Int32 Members_Id,
			            DateTime Order_Date,
			            Int32 Order_Quantity,
			            Double Order_Total,
			            String Shipping_Name,
			            String Shipping_Address,
			            String Shipping_Tel,
			            String Shipping_City,
			            String Shipping_Notice
                        )
                    {		_Order_Id=Order_Id;
		            _Members_Id=Members_Id;
		            _Order_Date=Order_Date;
		            _Order_Quantity=Order_Quantity;
		            _Order_Total=Order_Total;
		            _Shipping_Name=Shipping_Name;
		            _Shipping_Address=Shipping_Address;
		            _Shipping_Tel=Shipping_Tel;
		            _Shipping_City=Shipping_City;
		            _Shipping_Notice=Shipping_Notice;
                    }
                   
        	            public Int32 Order_Id
	                {
	                  get { return _Order_Id; }
	                  set { _Order_Id = value; }
	                 }

	            public Int32 Members_Id
	                {
	                  get { return _Members_Id; }
	                  set { _Members_Id = value; }
	                 }

	            public DateTime Order_Date
	                {
	                  get { return _Order_Date; }
	                  set { _Order_Date = value; }
	                 }

	            public Int32 Order_Quantity
	                {
	                  get { return _Order_Quantity; }
	                  set { _Order_Quantity = value; }
	                 }

	            public Double Order_Total
	                {
	                  get { return _Order_Total; }
	                  set { _Order_Total = value; }
	                 }

	            public String Shipping_Name
	                {
	                  get { return _Shipping_Name; }
	                  set { _Shipping_Name = value; }
	                 }

	            public String Shipping_Address
	                {
	                  get { return _Shipping_Address; }
	                  set { _Shipping_Address = value; }
	                 }

	            public String Shipping_Tel
	                {
	                  get { return _Shipping_Tel; }
	                  set { _Shipping_Tel = value; }
	                 }

	            public String Shipping_City
	                {
	                  get { return _Shipping_City; }
	                  set { _Shipping_City = value; }
	                 }

	            public String Shipping_Notice
	                {
	                  get { return _Shipping_Notice; }
	                  set { _Shipping_Notice = value; }
	                 }

        #endregion
                public static DataTable GetOrderNotOrder()
                {
                    DataTable tbl = new DataTable();
                    try
                    {
                        Access.InitAccess();
                        Access.mySqlCommand.CommandText = "Sp_Orders_Order_Getnotorder";
                        DataSet ds = new DataSet();
                        SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                        da.Fill(ds);
                        Access.ClosedAccess();
                        return ds.Tables[0];
                    }
                    catch {
                        Access.ClosedAccess();
                        return tbl;
                    }
                }
                public static bool Delete_Order(Int16 Order_Id)
                {
                    try {
                        Access.InitAccess();
                        Access.mySqlCommand.CommandText = "Sp_ShoppingCart_DeleteOrder";
                        Access.mySqlCommand.Parameters.Add("@Order_Id", SqlDbType.Int).Value = Order_Id;
                        int resutl = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                        Access.ClosedAccess();
                        if (resutl > 0) return true;
                        else return false;
                    }
                    catch {
                        Access.ClosedAccess();
                        return false;
                    }
                }
                public static bool Update_UpdateActive(Int16 Order_Id)
                {
                    //try
                    //{
                        Access.InitAccess();
                        Access.mySqlCommand.CommandText = "Sp_Order_UpdateIsActiveOrder";
                        Access.mySqlCommand.Parameters.Add("@Order_Id", SqlDbType.Int).Value = Order_Id;
                        int resutl = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                        //Access.ClosedAccess();
                        if (resutl > 0)
                        {
                            DataTable tbl = new DataTable();
                            DataSet ds = new DataSet();
                            Access.mySqlCommand.Parameters.Clear();
                            Access.mySqlCommand.CommandText = "Sp_Orders_GetProductByOrder_Id";
                            Access.mySqlCommand.Parameters.Add("@Orders_Id", SqlDbType.Int).Value = Order_Id;
                            SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                            da.Fill(ds);
                            tbl = ds.Tables[0];
                            foreach (DataRow row in tbl.Rows)
                            {
                                Access.mySqlCommand.Parameters.Clear();
                                Access.mySqlCommand.CommandText = "Sp_Products_UpdateQuantity";
                                Access.mySqlCommand.Parameters.Add("@Products_Id", SqlDbType.Int).Value = Convert.ToInt16(row["Products_id"].ToString());
                                Access.mySqlCommand.Parameters.Add("@Quantity", SqlDbType.Int).Value = Convert.ToInt16(row["Quantity"].ToString());
                                Int16 kg = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                                if (kg == 0)
                                {
                                    Access.ClosedAccess();
                                    return false;
                                }
                            }
                            Access.ClosedAccess();
                            return true;

                        }                       
                        else 
                        {
                            return false;
                        }
                    //}
                    //catch
                    //{
                    //    Access.ClosedAccess();
                    //    return false;
                    //}
                }
                public static Orders Getorderdetail(Int16 Order_Id)
                {
                    Orders myorder = new Orders();
                    SqlDataReader reader;
                    //try
                    //{
                        Access.InitAccess();
                        Access.mySqlCommand.CommandText = "Sp_Orders_GetDetail";
                        Access.mySqlCommand.Parameters.Add("@Order_Id", SqlDbType.Int).Value = Order_Id;
                        reader = Access.mySqlCommand.ExecuteReader();
                        while (reader.Read())
                        {
                            myorder.Order_Id = Convert.ToInt16(reader["Order_Id"].ToString());
                            myorder.Members_Id = Convert.ToInt16(reader["Members_Id"].ToString());
                            myorder.Order_Date = Convert.ToDateTime(reader["Order_Date"]);
                            myorder.Order_Quantity = Convert.ToInt16(reader["Order_Quantity"].ToString());
                            myorder.Order_Total = Convert.ToInt32(reader["Order_Total"].ToString());
                            myorder.Shipping_Address = reader["Shipping_Address"].ToString();
                            myorder.Shipping_City = reader["Shipping_City"].ToString();
                            myorder.Shipping_Tel = reader["Shipping_Tel"].ToString();
                            myorder.Shipping_Name = reader["Shipping_Name"].ToString();
                        }
                        Access.ClosedAccess();
                        return myorder;
                    //}catch
                    //{
                    //    Access.ClosedAccess();
                    //    return myorder;
                    //}
                }
                public static DataTable GetOrderByMember(int Members_Id)
                {
                    DataTable tbl = new DataTable();
                    try
                    {
                        Access.InitAccess();
                        Access.mySqlCommand.CommandText = "Sp_Orders_GetOrderByMember";
                        Access.mySqlCommand.Parameters.Add("@Members_Id", SqlDbType.Int).Value = Members_Id;
                        DataSet ds = new DataSet();
                        SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                        da.Fill(ds);
                        Access.ClosedAccess();
                        return ds.Tables[0];
                    }
                    catch
                    {
                        Access.ClosedAccess();
                        return tbl;
                    }
                }
                public static DataTable GetOrderByIsactive(int IsActive)
                {
                    DataTable tbl = new DataTable();
                    try
                    {
                        Access.InitAccess();
                        Access.mySqlCommand.CommandText = "Orders_GetOrderByActive";
                        Access.mySqlCommand.Parameters.Add("@IsActive", SqlDbType.Int).Value = IsActive;
                        DataSet ds = new DataSet();
                        SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                        da.Fill(ds);
                        Access.ClosedAccess();
                        return ds.Tables[0];
                    }
                    catch
                    {
                        Access.ClosedAccess();
                        return tbl;
                    }
                }
    }
}
